package cn.semcorp.zhuhaizj.common;

import cn.semcorp.zhuhaizj.exception.ServiceException;
import cn.semcorp.zhuhaizj.pojo.dto.PulpingDateDTO;
import cn.semcorp.zhuhaizj.restful.JsonResult;
import cn.semcorp.zhuhaizj.restful.ResponseCode;
import com.mchange.v1.identicator.IdList;
import org.apache.poi.ss.usermodel.DateUtil;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.IsoFields;
import java.util.*;

/**
 * 全局通用类
 */
public class Common {

    /**
     * 字符通用
     */
    public static final String DEFAULT_PAGE = "1";
    public static final String DEFAULT_PAGE_SIZE = "20";
    public static String commonSlurryName= "OM209";
    public static String icon1= "○";
    public static String icon2= "●";
    public static String icon3= "△";
    public static String common1= "已完成";
    public static String common2= "待完成";
    public static String common3= "奖";
    public static String common4= "罚";
    public static String common5= "合格";
    public static String common9= "不合格";
    public static String common6= "主操";
    public static String common7= "班长";
    public static String common8= "操作工";
    public static String common99= "污水间";
    public static String common98= "污水处理员";
    public static String common97= "制浆";
    public static String common855= "管理层";
    public static String common96= "亥";
    public static String common95= "班次";
    public static String common94= "日次";
    public static double common93= 365;
    public static String common92= "★★★★★";
    public static String common91= "★★★★";
    public static String common90= "★★★";
    public static String common89= "★★";
    public static String common88= "★";
    public static String common87= "制浆";
    public static String common86= "/";
    public static String common85= "管理者";
    public static String shift1= "甲";
    public static String shift2= "乙";
    public static String commonEligibleNO= "否";
    public static String commonEligibleYES= "是";
    public static String returnProp1 = "绩效累计扣比：0;";
    public static String returnProp2 = "绩效累计扣分：0;";
    public static String returnProp3 = "降级记录：无降级记录;";
    public static String returnProp4 = "累计罚款：0;";
    public static String returnProp5 = "累计现金奖励：0;";
    public static String returnProp6 = "绩效累计加分：0;";



    public static String commonPulpingA1Url= "/Users/woaini/Desktop/制浆A1车间统计表.xlsx";
    public static String commonPulpingA2Url= "/Users/woaini/Desktop/制浆A2车间统计表.xlsx";
    public static String commonPulpingBUrl= "/Users/woaini/Desktop/制浆B车间统计表.xlsx";
    public static String commonPulpingCUrl= "/Users/woaini/Desktop/制浆C车间统计表.xlsx";
    public static String commonPulpingMUrl= "/Users/woaini/Desktop/制浆M车间统计表.xlsx";
    public static List<Integer> weekList = new ArrayList<Integer>();

    public static String commonCoatingA2Url= "/Users/woaini/Desktop/涂布A2车间浆料使用记录表.xlsx";
    public static String commonCoatingA1Url= "/Users/woaini/Desktop/涂布A1车间浆料使用记录表.xlsx";
    public static String commonCoatingBSmallUrl= "/Users/woaini/Desktop/涂布B小车间浆料使用记录表.xlsx";
    public static String commonCoatingBBigUrl= "/Users/woaini/Desktop/涂布B大车间浆料使用记录表.xlsx";
    public static String commonCoatingCUrl= "/Users/woaini/Desktop/涂布C车间浆料使用记录表.xlsx";
    public static String commonCoatingDUrl= "/Users/woaini/Desktop/涂布D车间浆料使用记录表.xlsx";
    public static String commonCoatingOnlineUrl= "/Users/woaini/Desktop/涂布在线车间浆料使用记录表.xlsx";
    public static double value1= 1143;
    public static double value2= 1000;
    public static double value3= 0.98;
    public static double value4= 1.00;
    public static double value5= 365;
    public static String value6= "正式员工";
    public static String objValue1= "临时计划";
    public static String objValue2= "固定计划";
    public static String objValue3= "待完成";
    public static String objValue4= "教育培训";
    public static String objValue5= "技能认定";
    public static String objValue6= "等待结果";
    public static String objValue66= "制浆";
    public static String objValue67= "污水间";
    public static String objValue68= "试用期";
    public static String objValue69= "能力打造";
    public static String objValue70= "已完成";
    public static String objValue72= "了解";
    public static String objValue73= "专项计划";
    public static boolean objValue71= true;
    public static boolean objValue74= false;

    // 机台
    public static String machineT01= "T01";
    public static String machineT02= "T02";
    public static String machineT03= "T03";
    public static String machineT04= "T04";
    public static String machineT05= "T05";
    public static String machineT06= "T06";
    public static String machineT07= "T07";
    public static String machineT08= "T08";
    public static String machineT09= "T09";
    public static String machineT10= "T10";
    public static String machineT16= "T16";
    public static String machineT17= "T17";
    public static String machineT18= "T18";
    public static String machineT19= "T19";
    public static String machineT20= "T20";
    public static String machineT21= "T21";
    public static String machineT22= "T22";
    public static String machineT23= "T23";
    public static String machineT24= "T24";
    public static String machineT25= "T25";
    public static String machineT26= "T26";
    public static String machineT29= "T29";
    public static String machineT30= "T30";
    public static String machineT31= "T31";
    public static String machineT32= "T32";
    public static String machineT33= "T33";
    public static String machineT34= "T34";
    public static String machineT35= "T35";
    public static String machineT36= "T36";
    public static String machineT37= "T37";
    public static String machineZM= "ZM";
    public static String machineZP= "ZP";
    public static String machineZO= "ZO";
    public static String machineZN= "ZN";
    public static String modelA= "油性";
    public static String modelB= "SRS";
    public static String modelC= "PVDF-喷涂";
    public static String modelD= "陶瓷";
    public static String modelE= "芳纶";
    public static String workshop1= "涂布A1车间";
    public static String workshop2= "涂布A2车间";
    public static String workshop3= "涂布B小车间";
    public static String workshop4= "涂布B大车间";
    public static String workshop5= "涂布C车间";
    public static String workshop6= "涂布D车间";
    public static String workshop7= "涂布在线车间";



    // 正则表达式1
    public static String patternStr = "\\d{4}-\\d{2}-\\d{2}";
    public static String regex = "\\d+";
    public static List<String> getModelAssessList(){
        List<String> list = new ArrayList<>();
        list.add(common3);
        list.add(common4);
        return list;
    }


    public static List<String> getModelAssessColumnList(){
        List<String> list = new ArrayList<>();
        list.add("performance_deduct");
        list.add("performance_assess");
        list.add("fines");
        list.add("reward");
        list.add("performance_bonus_points");
        return list;
    }

    public static List<String> getModelCountryList(){
        List<String> list = new ArrayList<>();
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        return list;
    }

    /**
     * 集合通用
     */
    public static List<String> machineNameList = new ArrayList<>();
    public static List<String> urlPulpingList = new ArrayList<>();
    public static List<String> urlCoatingList = new ArrayList<>();


    public static String formatters(double excelDateValue){
        java.util.Date javaDate = DateUtil.getJavaDate(excelDateValue);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(javaDate);
    }
    /**
     * 方法通用
     */
    // 日期与现在比多少天
    public static boolean howMuchDaysIn6Months(String completed){
        String nowDate = String.valueOf(LocalDate.now());

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");

        // 将日期字符串转换为LocalDate对象
        LocalDate date1 = LocalDate.parse(nowDate, formatter);
        LocalDate date2 = LocalDate.parse(completed, formatter);
        // 比较两个日期
        long daysBetween = ChronoUnit.DAYS.between(date2,date1);
        if (daysBetween >= 180){
            return true;
        }
        return false;
    }
    // 拆分
    public static <T> List<List<T>> splitListBySize(List<T> list, int batchSize) {
        List<List<T>> batchedList = new ArrayList<>();
        int size = list.size();
        for (int i = 0; i < size; i += batchSize) {
            int end = Math.min(i + batchSize, size);
            batchedList.add(list.subList(i, end));
        }
        return batchedList;
    }
    // 跟现在比较日期有有多少天
    public static long howMuchDaysBetween(String date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate parse = LocalDate.parse(date, formatter);
        return ChronoUnit.DAYS.between(parse,Common.getBeforeNowDate());
    }
    public static long howMuchDaysBetween1(String date){
        LocalDate parse = LocalDate.parse(date);
        LocalDate now = Common.getBeforeNowDate();
        return ChronoUnit.DAYS.between(parse,now);
    }

    // 判断两个日期之间的天数
    public static String howManyDaysBetween(String startDate,String endDate){
        if (startDate == null || endDate == null){
            return "";
        }
        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        return String.valueOf(ChronoUnit.DAYS.between(start,end));
    }

    // 获取全部涂布领用的excel表名
    public static List<String> getCommonCoatingUrlList () {
        urlCoatingList = new ArrayList<>();

        urlCoatingList.add(commonCoatingA1Url);
        urlCoatingList.add(commonCoatingA2Url);
        urlCoatingList.add(commonCoatingBSmallUrl);
        urlCoatingList.add(commonCoatingBBigUrl);
        urlCoatingList.add(commonCoatingCUrl);
        urlCoatingList.add(commonCoatingDUrl);
        urlCoatingList.add(commonCoatingOnlineUrl);
        return urlCoatingList;
    }

    public static List<String> getSchemaList(){
        List<String> list = new ArrayList<>();
        list.add("主任");
        list.add("工程师");
        list.add("9S");
        list.add("文员");
        list.add("班长");
        list.add("主操");
        list.add("操作工");
        return list;
    }
    public static List<String> getSchemaLi(){
        List<String> list = new ArrayList<>();
        list.add("主任");
        list.add("工程师");
        list.add("9S");
        list.add("文员");
        list.add("数据");
        list.add("班长");
        list.add("主操");
        list.add("操作工");
        return list;
    }

    public static List<String> getShiftList(){
        List<String> list = new ArrayList<>();
        list.add("甲");
        list.add("乙");
        return list;
    }
    public static String formatterDate(String year,String month){
        int yearInt = Integer.parseInt(year);
        int monthInt = Integer.parseInt(month);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
        try {
            Date date = sdf.parse(yearInt + "/" + monthInt);
            return sdf.format(date);
        } catch (ParseException e) {
            throw new ServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"解析错误,查询失败");
        }
    }

    public static String formatterDateYYYY_MM(String year,String month){
        int yearInt = Integer.parseInt(year);
        int monthInt = Integer.parseInt(month);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        try {
            Date date = sdf.parse(yearInt + "-" + monthInt);
            return sdf.format(date);
        } catch (ParseException e) {
            throw new ServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"解析错误,查询失败");
        }
    }
    public static List<String> getWorkshopList(){
        List<String> list = new ArrayList<>();
        list.add("A1");
        list.add("A2");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("污水间");
        return list;
    }

    public static List<String> getWorkshopList1(){
        List<String> list = new ArrayList<>();
        list.add("A1");
        list.add("A2");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("污水间");
        list.add("制浆");
        return list;
    }

    public static List<String> getWorkshopLi(){
        List<String> list = new ArrayList<>();
        list.add("A1");
        list.add("A2");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("污水间");
        return list;
    }
    // 获取全部制浆领用的excel表名
    public static List<String> getCommonPulpingUrlList () {
        urlPulpingList = new ArrayList<>();

        urlPulpingList.add(commonPulpingA1Url);
        urlPulpingList.add(commonPulpingA2Url);
        urlPulpingList.add(commonPulpingBUrl);
        urlPulpingList.add(commonPulpingCUrl);
        urlPulpingList.add(commonPulpingMUrl);
        return urlPulpingList;
    }

    // 获取当天的日期
    public static LocalDate getNowDate(){
        return LocalDate.now();
    }



    /**
     * 辅助 - 物料类型
     */
    public static String whatIsMaterialsModel(String materials){
        if (materials == null){
            return "";
        }
        if (materials.contains("涂布")){
            return "浆料";
        }
        return "原料";
    }
    /**
     * 辅助 - 原料是否过期
     */
    public static String materialsIsNormal(String useDate) {
        if (useDate == null){
            return "";
        }
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 将字符串转换为 LocalDate 对象
        LocalDate use = LocalDate.parse(useDate, formatter);
        if (use.isAfter(LocalDate.now())){
            return "正常";
        }
        return "过期";
    }

    public static String getNowDateString(){
        return String.valueOf(LocalDate.now());
    }

    // 获取全部的浆料类型
    public static List<String> getModelList(){
        List<String> modelList = new ArrayList<>();
        modelList.add(modelA);
        modelList.add(modelB);
        modelList.add(modelC);
        modelList.add(modelD);
        modelList.add(modelE);
        return modelList;
    }

    // 获取近十天时间集合
    public static List<String> getDateOneBetweenTen(){
        List<String> tenDate = new ArrayList<>();
        for (int i=0;i<=9;i++){
            String dateByNum = String.valueOf(getDateByNum(i));
            tenDate.add(dateByNum);
        }
        return tenDate;
    }

    // 获取近3十天时间集合
    public static List<String> getDateOneBetween3Ten(){
        List<String> tenDate = new ArrayList<>();
        for (int i=0;i<=29;i++){
            String dateByNum = String.valueOf(getDateByNum(i));
            tenDate.add(dateByNum);
        }
        return tenDate;
    }

    // 获取所有的涂布车间
    public static List<String> getAllWorkshop(){
        List<String> workshopList = new ArrayList<>();
        workshopList.add(workshop1);
        workshopList.add(workshop2);
        workshopList.add(workshop3);
        workshopList.add(workshop4);
        workshopList.add(workshop5);
        workshopList.add(workshop6);
        workshopList.add(workshop7);
        return workshopList;
    }

    // 判断是否为数字
    public static boolean isNumber(String number){
        try{
            Double.parseDouble(number);
            return true;
        }catch (Throwable e){
            return false;
        }
    }

    // 获取近六周
    public static List<String> getSixWeek(){
        LocalDate date = Common.getBeforeNowDate();
        String beforeNowDate = String.valueOf(date);
        LocalDate inputDate = LocalDate.parse(beforeNowDate);
        // 当前周次
        int weekNumber = inputDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
        int year = inputDate.getYear();
        List<String> weekList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            String week = String.valueOf(year).concat(String.valueOf(weekNumber));
            weekList.add(week);
            // 判断
            if (weekNumber - 1 <= 0){
                year -- ;
                weekNumber = 52;
                continue;
            }
            weekNumber--;
        }
        Collections.reverse(weekList);
        return weekList;
    }

    // 获取近12个月
    public static List<String> getTenTwoMonth(){
        List<String> monthList = new ArrayList<>();
        LocalDate beforeNowDate = Common.getBeforeNowDate();
        YearMonth previousYearMonth = YearMonth.from(beforeNowDate);
        for (int i = 0; i < 12; i++) {
            //previousYearMonth = previousYearMonth.minusMonths(i);
            String d = String.valueOf(previousYearMonth);
            monthList.add(d);
            // 判断是否需要跨年
            int year = previousYearMonth.getYear();
            int monthValue = previousYearMonth.getMonthValue();
            if (monthValue == 1) {
                year--;
                monthValue = 12;
                previousYearMonth = YearMonth.of(year,monthValue);
            }else{
                monthValue --;
                previousYearMonth = YearMonth.of(year,monthValue);
            }
        }
        Collections.reverse(monthList);
        return monthList;
    }

    public static List<String> getLastSixMonthsInYYYYMMFormat() {
        List<String> lastSixMonths = new ArrayList<>();
        LocalDate currentDate = Common.getBeforeNowDate();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        for (int i = 0; i <= 5; i++) {
            LocalDate month = currentDate.minusMonths(i);
            String formattedMonth = month.format(formatter);
            lastSixMonths.add(formattedMonth);
        }
        Collections.reverse(lastSixMonths);
        return lastSixMonths;
    }

    // 转化百分比
    public static String transformPercent (double number) {
        DecimalFormat decimalFormat = new DecimalFormat("0.##%");
        return decimalFormat.format(number);
    }

    public static String transformPercentByString (String number) {
        DecimalFormat decimalFormat = new DecimalFormat("0.##%");
        return decimalFormat.format(number);
    }

    // 保留两位小数
    public static String transformTwoNumber (double number) {
        DecimalFormat decimalFormat = new DecimalFormat("0.##");
        return decimalFormat.format(number);
    }

    public static double transformTwoNumberDouble (double number) {
        DecimalFormat decimalFormat = new DecimalFormat("0.##");
        return Double.parseDouble(decimalFormat.format(number));
    }

    // 保留一位小数
    public static String transformOneNumber (double number) {
        DecimalFormat decimalFormat = new DecimalFormat("0.#");
        return decimalFormat.format(number);
    }

    // 获取全部能力项
    public static List<String> getAllAbles(){
        List<String> list = new ArrayList<>();
        list.add("管理");
        list.add("安全");
        list.add("技能");
        list.add("SOP(通用)");
        list.add("CP(陶瓷)");
        list.add("CP(油性)");
        list.add("CP(芳纶)");
        list.add("CP(PVDF)");
        list.add("CP(SRS)");
        return list;
    }


    // 根据仓库编码查询车间
    public static String getWorkByWarehouse(String warehouse){
        if (warehouse.equals("ZHCK57") || warehouse.equals("ZHCK58")){
            return "A1";
        }
        if (warehouse.equals("ZHCK59")){
            return "A2";
        }
        if (warehouse.equals("ZHCK11")){
            return "B";
        }
        if (warehouse.equals("ZHCK60")){
            return "C";
        }
        if (warehouse.equals("ZHCK61")){
            return "D";
        }
        if (warehouse.equals("ZHCK76")){
            return "E";
        }
        throw new ServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"表存在仓库编码无对应车间");
    }


    // 获取全部的机台名称
    /**
     * 7 8 9 10
     * @return
     */
    public static List<String> getCommonMachineList(){
        machineNameList = new ArrayList<>();
        machineNameList.add(machineT01);
        machineNameList.add(machineT02);
        machineNameList.add(machineT03);
        machineNameList.add(machineT04);
        machineNameList.add(machineT05);
        machineNameList.add(machineT06);
        machineNameList.add(machineT07);
        machineNameList.add(machineT08);
        machineNameList.add(machineT09);
        machineNameList.add(machineT10);
        machineNameList.add(machineT16);
        machineNameList.add(machineT17);
        machineNameList.add(machineT18);
        machineNameList.add(machineT19);
        machineNameList.add(machineT20);
        machineNameList.add(machineT21);
        machineNameList.add(machineT22);
        machineNameList.add(machineT23);
        machineNameList.add(machineT24);
        machineNameList.add(machineT25);
        machineNameList.add(machineT26);
        machineNameList.add(machineT29);
        machineNameList.add(machineT30);
        machineNameList.add(machineT31);
        machineNameList.add(machineT32);
        machineNameList.add(machineT33);
        machineNameList.add(machineT34);
        machineNameList.add(machineT35);
        machineNameList.add(machineT36);
        machineNameList.add(machineT37);
        machineNameList.add(machineZM);
        machineNameList.add(machineZN);
        machineNameList.add(machineZO);
        machineNameList.add(machineZP);
        return machineNameList;
    }

    // 获取前一天日期
    public static LocalDate getBeforeNowDate() {
        // 今天的日期
        LocalDate nowDate = LocalDate.now();
        // 前一天的日期
        return nowDate.minusDays(1);
    }

    public static LocalDate getBeforeTenDate() {
        // 今天的日期
        LocalDate nowDate = LocalDate.now();
        // 前一天的日期
        return nowDate.minusDays(11);
    }

    // 获取前三十天的日期
    public static LocalDate getBefore3TenNowDate() {
        // 今天的日期
        LocalDate nowDate = LocalDate.now();
        // 前一天的日期
        return nowDate.minusDays(31);
    }

    // 获取两个日期之间的全部日期
    public static List<String> getAllDatesBetweenTwoDates(LocalDate startDate, LocalDate endDate) {
        List<String> dates = new ArrayList<>();
        LocalDate currentDate = startDate;
        while (!currentDate.isAfter(endDate)) {
            dates.add(String.valueOf(currentDate));
            currentDate = currentDate.plusDays(1);
        }
        return dates;
    }

    // 获取当前日期任意一天
    public static LocalDate getDateByNum(int number) {
        // 今天的日期
        LocalDate nowDate = Common.getBeforeNowDate();
        return nowDate.minusDays(number);
    }



    // 返回一个周次集合
    public static List<Integer> getWeekList(){
        weekList = new ArrayList<>();
        LocalDate beforeNowDate = Common.getBeforeNowDate();
        Integer week = beforeNowDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
        Integer week1 = beforeNowDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)-1;
        Integer week2 = beforeNowDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)-2;
        Integer week3 = beforeNowDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)-3;
        weekList.add(week3);
        weekList.add(week2);
        weekList.add(week1);
        weekList.add(week);
        return weekList;
    }

}
